package com.dm.cloud.oauthlib.service.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class AopUtil {

    /**
     * 通过连接点对象获取其方法或类上的注解
     *
     * @param point  连接点
     * @param tClass 注解类型
     * @return T 注解
     * @date 2023/2/21
     */
    public static  <T extends Annotation> T getAnnotation(JoinPoint point, Class<T> tClass) {
        T t = null;
        Signature signature = point.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null && method.isAnnotationPresent(tClass)) {
            t = method.getAnnotation(tClass);
        }
        if (t == null && method.getDeclaringClass().isAnnotationPresent(tClass)) {
            t = method.getDeclaringClass().getAnnotation(tClass);
        }
        return t;
    }

}
